#include "mainpage.h"


uint8_t gamestart = 0;
uint8_t keyword[4] = {0};
uint8_t KeyWordDet(void)
{
    static uint8_t keyup = 0;
    uint8_t keynums = 0;
    UART_printf(&huart1, "%d%d%d%d\n", keyword[0], keyword[1], keyword[2], keyword[3]);
    for (int i = 1; i < 17; i++)
        keynums += GetKeyVal(i);
    if (!keynums)
        keyup = 1; //按键松开

    if ((keynums == 1) && keyup)
    {
        keyup = 0;
        for (int i = 0; i < 3; i++)
            keyword[i] = keyword[i + 1];
        for (int i = 1; i < 17; i++)
            if (GetKeyVal(i))
                keyword[3] = i;
    }
    if ((keyword[0] == 4) && (keyword[1] == 3) && (keyword[2] == 1) && (keyword[3] == 4))
    {
        keyword[0]=0;
        // BBstop();
        gamestart = 1;
        return 1;
    }
    return 0;
}

extern uint16_t BBscale[16];

void MPdis(void)
{
    u8g2_FirstPage(&oled);
    do
    {
        u8g2_SetFont(&oled, u8g2_font_10x20_mr);
        u8g2_DrawStr(&oled, 0, 18, "Board");
        u8g2_DrawStr(&oled, 5, 36, "demo");
        u8g2_DrawStr(&oled, 0, 60, "CAST");
        for (int j = 1; j < 4; j++)
        {
            u8g2_DrawHLine(&oled, 0 + 64, 16 * j, 64);
            u8g2_DrawHLine(&oled, 0 + 64, 16 * j - 1, 64);
            u8g2_DrawVLine(&oled, 16 * j + 64, 0, 64);
            u8g2_DrawVLine(&oled, 16 * j - 1 + 64, 0, 64);
        }
        u8g2_DrawFrame(&oled, 0 + 64, 0, 64, 64);
        for (int j = 0; j < 16; j++)
            if (GetKeyVal(j + 1))
                u8g2_DrawBox(&oled, 16 * (j % 4) + 2 + 64, 16 * (j / 4) + 2, 12, 12);
        if (GetKeyVal(17))
            u8g2_DrawBox(&oled, 48, 34, 12, 12);
        u8g2_DrawRFrame(&oled, 47, 33, 14, 14, 1);
        if (GetKeyVal(18))
            u8g2_DrawBox(&oled, 48, 50, 12, 12);
        u8g2_DrawRFrame(&oled, 47, 49, 14, 14, 1);
    } while (u8g2_NextPage(&oled));

    uint8_t iskey = 0;
    for (int j = 0; j < 16; j++)
    {
        if (GetKeyVal(j + 1))
        {
            BBfrec(BBscale[j]);
            iskey = 1;
        }
    }
    if (!iskey)
        BBstop();
}
